/**
 *******************************************************************************
 * @FileName  : tlsr825x_pwm.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#ifndef TLSR825X_PWM_H_
#define TLSR825X_PWM_H_

#include "tlsr825x.h"

/*!  PWM define */
#define PWM0    0
#define PWM1    1
#define PWM2    2
#define PWM3    3
#define PWM4    4
#define PWM5    5

/*!  PWM Mode */
#define PWM_MODE_NORMAL          (uint8_t)0x00
#define PWM_MODE_CNT             (uint8_t)0x01
#define PWM_MODE_IR              (uint8_t)0x03
#define PWM_MODE_IR_FIFO         (uint8_t)0x07
#define PWM_MODE_IR_DMA          (uint8_t)0x0F


/*! PWM Interrupt . */
#define PWM_IT_Cnt               (uint16_t)(0x0001) /*!< only use for PWM0 */
#define PWM_IT_Dma               (uint16_t)(0x0002) /*!< only use for PWM0 */
#define PWM_IT_Frame0            (uint16_t)(0x0004) /*!< only use for PWM0 */
#define PWM_IT_Frame1            (uint16_t)(0x0008) /*!< only use for PWM1 */
#define PWM_IT_Frame2            (uint16_t)(0x0010) /*!< only use for PWM2 */
#define PWM_IT_Frame3            (uint16_t)(0x0020) /*!< only use for PWM3 */
#define PWM_IT_Frame4            (uint16_t)(0x0040) /*!< only use for PWM4 */
#define PWM_IT_Frame5            (uint16_t)(0x0080) /*!< only use for PWM5 */
#define PWM_IT_Fifo              (uint16_t)(0x0100) /*!< only use for PWM0 */

/*! PWM Interrupt Flag. */
#define PWM_IT_Flag_Cnt          (uint16_t)(0x0001) /*!< only use for PWM0 */
#define PWM_IT_Flag_Dma          (uint16_t)(0x0002) /*!< only use for PWM0 */
#define PWM_IT_Flag_Frame0       (uint16_t)(0x0004) /*!< only use for PWM0 */
#define PWM_IT_Flag_Frame1       (uint16_t)(0x0008) /*!< only use for PWM1 */
#define PWM_IT_Flag_Frame2       (uint16_t)(0x0010) /*!< only use for PWM2 */
#define PWM_IT_Flag_Frame3       (uint16_t)(0x0020) /*!< only use for PWM3 */
#define PWM_IT_Flag_Frame4       (uint16_t)(0x0040) /*!< only use for PWM4 */
#define PWM_IT_Flag_Frame5       (uint16_t)(0x0080) /*!< only use for PWM5 */
#define PWM_IT_Flag_Fifo         (uint16_t)(0x0100) /*!< only use for PWM0 */


/********** PWM common API ***********/

/**
 * @brief  : PWM initliazation.
 * @param  : PWM_ClkDiv       clock div.
 * @return : None.
 */
void PWM_Init(uint8_t PWM_ClkDiv);

/**
 * @brief  : Set PWM pulse parameter.
 * @param  : PWMx        can be PWM0/PWM1/PWM2...
 * @param  : duty        PWM duty,  unit: the number of PWM clock tick.
 * @param  : cycle       PWM cycle, unit: the number of PWM clock tick.
 * @return : None.
 */
void PWM_SetPulse(uint8_t PWMx, uint16_t duty, uint16_t cycle);

/**
 * @brief  : Set PWM pulse2 parameter.
 *           !!!Note: PWM can support two pulses with different frequency parameters
 *                    at the same time. The transmission is controlled by the corresponding enable bit.
 *
 * @param  : PWMx        only use for PWM0.
 * @param  : duty        PWM duty,  unit: the number of PWM clock tick.
 * @param  : cycle       PWM cycle, unit: the number of PWM clock tick.
 * @return : None.
 */
void PWM_SetPulse2(uint8_t PWMx, uint16_t duty, uint16_t cycle);

/**
 * @brief  : Set PWM mode.
 * @param  : PWMx        only use for PWM0.
 * @param  : PWM_Mode    can be PWM_MODE_NORMAL/PWM_MODE_CNT...
 * @return : None.
 */
void PWM_SetMode(uint8_t PWMx, uint8_t PWM_Mode);

/**
 * @brief  : Set PWM pulse number.
 * @param  : PWMx        only use for PWM0.
 * @param  : num         the number of PWM pulse.
 * @return : None.
 */
void PWM_SetPulseNum(uint8_t PWMx, uint16_t num);

/**
 * @brief  : Set PWM enable.
 * @param  : PWMx        can be PWM0/PWM1/PWM2....
 * @return : None.
 */
void PWM_Enable(uint8_t PWMx);

/**
 * @brief  : Set PWM enable/disable.
 * @param  : PWMx        can be PWM0/PWM1/PWM2....
 * @return : None.
 */
void PWM_Disable(uint8_t PWMx);

/**
 * @brief  : Set PWM interrupt.
 * @param  : PWMx        can be PWM0/PWM1...
 * @param  : PWM_IT      can be PWM_IT_Cnt/PWM_IT_Dma/PWM_IT_Frame0...
 * @param  : enable      enable / disable PWM interrupt.
 * @return : None.
 */
void PWM_ITConfig(uint8_t PWMx, uint8_t PWM_IT, bool_t enable);

/**
 * @brief  : Get PWM interrupt.
 * @param  : PWMx          can be PWM0/PWM1...
 * @param  : PWM_IT_Flag   can be PWM_IT_Flag_Cnt/PWM_IT_Flag_Dma/PWM_IT_Flag_Frame0...
 * @return : 1 if interrupt is valid or 0.
 */
uint8_t PWM_GetITFlag(uint8_t PWMx, uint8_t PWM_IT_Flag);

/**
 * @brief  : Clear PWM interrupt flag.
 * @param  : PWMx          can be PWM0/PWM1...
 * @param  : PWM_IT_Flag   can be PWM_IT_Flag_Cnt/PWM_IT_Flag_Dma/PWM_IT_Flag_Frame0...
 * @return : None.
 */
void PWM_ClearITFlag(uint8_t PWMx, uint8_t PWM_IT_Flag);


/********** PWM DMA mode API ***********/

/**
 * @brief  : Set PWM wave data to DMA. only use for PWM0
 * @param  : pWaveForm          Pointer point to PWM wave data buffer.
 *                            # DMA data format:
 *                               LSB   | dma length       |   pwm wave data           | MSB
 *                                     | 4B               |     N bytes               |
 *                               !!![dma length == N]
 *                            # pwm wave data format:
 *                               MSB  | pulse enable bit  | pulse select bit | the number of pulse | LSB
 *                                    |  1bit             |  1bit            |       14bit         |
 *                               !!!Note: pulse enable bit = 1  --> PWM wave will be send.
 *                                        pulse select bit = 1  --> pulse2 will be used.
 *                                        pulse select bit = 0  --> pulse  will be used.
 *
 * @param  : byteLen            the length of bytes of wave data.
 *                              !!! MAX length = 512B.
 * @return : None.
 */
void PWM_DmaSetData(uint16_t *pWaveForm, uint32_t byteLen);

/**
 * @brief  : Set PWM DMA mode enable/disable and enable PWM.
 * @param  : PWMx        only use for PWM0.
 * @return : None.
 */
void PWM_DmaEnable(void);


#endif /* TLSR825X_PWM_H_ */



